<template>
  <div class="assets">
    <div>
      <el-button type="primary" size="mini" @click="save">{{ $t('btn.save') }}</el-button>
    </div>
    <div class="title">{{ $t('market.info') }}</div>
    <el-form :model="proValue" label-width="100px" size="mini" :rules="rules">
      <el-form-item :label="$t('market.assetstype')" prop="fassetstypeid">
        <el-select v-model="proValue.fassetstypeid">
          <el-option
            v-for="item in billtype"
            :key="item.fid"
            :label="item.fname"
            :value="item.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.assetsname')" prop="fassetsname">
        <el-input v-model="proValue.fassetsname" />
      </el-form-item>
      <el-form-item :label="$t('market.measuring')" prop="funitid">
        <el-input v-model="proValue.funitname">
          <i
            slot="suffix"
            class="el-input__icon el-icon-search"
            @click="isBaseUnit=true"
          />
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('market.assetQty')" prop="fassetsamount">
        <el-input-number v-model="proValue.fassetsamount" controls-position="right" />
      </el-form-item>
      <el-form-item :label="$t('market.assetStatus')">
        <el-select v-model="proValue.fstatusid">
          <el-option
            v-for="item in billtype3"
            :key="item.fid"
            :label="item.fname"
            :value="item.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.changeid')">
        <el-select v-model="proValue.fchangeid">
          <el-option
            v-for="item in billtype4"
            :key="item.fid"
            :label="item.fname"
            :value="item.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.note')">
        <el-input v-model="proValue.fremark" />
      </el-form-item>
      <el-form-item :label="$t('market.fmaterial')">
        <el-input v-model="proValue.fnumber">
          <i
            slot="suffix"
            class="el-input__icon el-icon-search"
            @click="isMateria=true"
          />
        </el-input>
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('market.fince')" name="first">
        <fince :fince.sync="proValue" />
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="second">
        <matter :table-data.sync="matter" :num="proValue.fassetsamount" />
      </el-tab-pane>
    </el-tabs>
    <material v-if="isMateria" fisasset="1" @material="materialData" />
    <baseUnit v-if="isBaseUnit" :msg="false" @unit="selBaseunit" />
  </div>
</template>

<script>
import {
  createAssetsBill
} from '@/api/financial/assets'
import { queryViewItem } from '@/api/marketManage/marketOrder'
import fince from '@/views/financial/assets/assetsAdd/commponent/fince'
import matter from '@/views/financial/assets/assetsAdd/commponent/matter'
import { mapGetters } from 'vuex'
export default {
  components: {
    fince,
    matter,
    baseUnit: () => import('@/views/basic/createMateriel2/components/baseUnit'),
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material')
  },
  data() {
    return {
      activeName: 'first',
      isMateria: false, // 物料
      isBaseUnit: false, // 物料
      proValue: {
        fnowdepmonths: 0,
        fstatusid: 1456,
        fchangeid: 1442,
        fassetsamount: 0,
        fpolicyid: '',
        fentrydate: new Date().toLocaleDateString().replaceAll('/', '-'),
        fexpectusedate: 1,
        fprice: 0,
        ftaxprice: 0,
        fcostprice: 0,
        ftaxcostprice: 0,
        fassetsoriginalprice: 0,
        fcumulativedepprice: 0,
        fassetsnetworthprice: 0,
        fbookfprice: 0,
        fexpectresidualfprice: 0,
        fdepdate: 0,
        fdepprice: 0,
        fcurrencyname: '美元',
        fcurrencyid: 7,
        fsettlecurrid: '',
        fsettlecurrname: '',
        exchangerate: '',
        fsettleprice: ''
      },
      billtype: [], // 资产类别
      billtype3: [], // 资产状态
      billtype4: [], // 资产状态
      matter: [], // 其他信息
      rules: {
        fassetstypeid: [{ required: true, message: '资产类别不能为空!', trigger: 'blur' }],
        fassetsname: [{ required: true, message: '资产名称不能为空!', trigger: 'blur' }],
        funitid: [{ required: true, message: '计量单位不能为空!', trigger: 'blur' }],
        fassetsamount: [{ required: true, message: '资产数量不能为空!', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['fcurrency'])
  },
  created() {
    this.proValue.fcurrencyid = this.fcurrency.fcurrencyId
    this.proValue.fcurrencyname = this.fcurrency.fcurrencyName
    this.queryTBasBilltype()
    this.queryTBasBilltype3()
    this.queryTBasBilltype4()
  },
  methods: {
    // 保存
    save() {
      this.staEnd = false
      this.usedep = false
      this.fstocknum = 0
      this.matter.forEach(res => {
        if (res.fuseenddate < res.fusestartdate) {
          this.staEnd = true
        }
        if (!res.fusedepname) {
          this.usedep = true
        }
        this.fstocknum += res.fstockamount
      })
      if (this.fstocknum !== this.proValue.fassetsamount) {
        this.$confirm('领用数量之和必须等于资产数量！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })

        return
      }
      if (this.staEnd) {
        this.$confirm('其他信息-结束日期不能小于开始日期,使用部门为必填项.', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        this.staEnd = false
        return
      }
      if (this.usedep) {
        this.$confirm('其他信息-使用部门、使用人为必填项!!!', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        this.usedep = false
        return
      }
      const DATA = {
        ...this.proValue,
        ...this.fince,
        details: this.matter
      }
      createAssetsBill(DATA).then(res => {
        if (res.code === 0) {
          this.$message({ showClose: true, message: res.message, type: 'success' })
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: `/assetsDetail/${res.data.fid}` })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 资产类别
    async queryTBasBilltype() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_ASSETS_TYPE' })
      this.billtype = RES.list
    },
    // 资产状态
    async queryTBasBilltype3() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_ASSETS_STATUS' })
      this.billtype3 = RES.list
    },
    // 变动方式
    async queryTBasBilltype4() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_ASSETS_CHANGE' })
      this.billtype4 = RES.list
    },
    // 物料编码
    materialData(item) {
      if (item.fmaterialId) {
        this.proValue.fmaterialid = item.fmaterialId
        this.proValue.fnumber = item.fnumber
      }
      this.isMateria = false
    },
    // 计量单位
    selBaseunit(ev) {
      if (ev.funitid) {
        this.proValue.funitname = ev.fname
        this.proValue.funitid = ev.funitid
      }
      this.isBaseUnit = false
    }
  }
}
</script>

<style scoped lang="scss">
.assets {
  @include listBom;
  .title {
    font-size: 14px;
    margin-top: 5px;
    color: #1e9fff;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      height: 30px !important;
      width: 275px;
      margin-bottom: 5px;
    }
  }
}
</style>

